package cn.iocoder.yudao.module.member.api.praxisProject;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.member.api.praxisProject.dto.*;
import cn.iocoder.yudao.module.member.controller.admin.praxisProject.vo.PraxisAnnexVO;
import cn.iocoder.yudao.module.member.controller.admin.praxisProject.vo.PraxisProjectPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.praxisProject.vo.PraxisProjectRespVO;
import cn.iocoder.yudao.module.member.controller.admin.praxisProject.vo.PraxisProjectScheduleVO;
import cn.iocoder.yudao.module.member.controller.admin.praxisProjectResult.vo.PraxisAnnexBaseVO;
import cn.iocoder.yudao.module.member.controller.admin.praxisProjectResult.vo.PraxisProjectResultPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.praxisProjectResult.vo.PraxisProjectResultRespVO;
import cn.iocoder.yudao.module.member.controller.admin.praxisProjectType.vo.PraxisProjectTypeExportReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.praxisProject.PraxisProjectDO;
import cn.iocoder.yudao.module.member.dal.dataobject.praxisProjectType.PraxisProjectTypeDO;
import cn.iocoder.yudao.module.member.service.praxisProject.PraxisProjectService;
import cn.iocoder.yudao.module.member.service.praxisProjectResult.PraxisProjectResultService;
import cn.iocoder.yudao.module.member.service.praxisProjectType.PraxisProjectTypeService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 项目 API 接口
 *
 */
@Service
public class PraxisProjectApiImpl implements PraxisProjectApi {
     @Resource
     private PraxisProjectService praxisProjectService;
     @Resource
     private PraxisProjectResultService praxisProjectResultService;

     @Resource
     private PraxisProjectTypeService praxisProjectTypeService;

     @Override
     public PageResult<PraxisProjectResultDTO> getPraxisProjectResultPage(PraxisProjectPageReqDTO pageReqDTO) {
          PraxisProjectResultPageReqVO pageReqVO=new PraxisProjectResultPageReqVO();
          BeanUtils.copyProperties(pageReqDTO,pageReqVO);
          PageResult<PraxisProjectResultRespVO> r = praxisProjectResultService.getProjectResultPage(pageReqVO);
          PageResult<PraxisProjectResultDTO> pageResult=new PageResult<>();
          List<PraxisProjectResultRespVO> list = r.getList();
          pageResult.setTotal(r.getTotal());
          if(CollectionUtils.isNotEmpty(list)){
               List<PraxisProjectResultDTO> rList = new ArrayList<>();
               list.stream().forEach(m->{
                    PraxisProjectResultDTO dto=new PraxisProjectResultDTO();
                    BeanUtils.copyProperties(m,dto);
                    rList.add(dto);
               });
               pageResult.setList(rList);
          }
          return pageResult;
     }

     @Override
     public PraxisProjectResultDTO getProjectResultById(Long id) {
          PraxisProjectResultRespVO result = praxisProjectResultService.getPraxisProjectResultById(id);
          PraxisProjectResultDTO dto=new PraxisProjectResultDTO();
          BeanUtils.copyProperties(result,dto);
          List<PraxisAnnexBaseVO> annexList = result.getAnnexList();
          if(CollectionUtils.isNotEmpty(annexList)){
               List<PraxisAnnexBaseDTO>  pList=new ArrayList<>();
               annexList.stream().forEach(m->{
                    PraxisAnnexBaseDTO a=new PraxisAnnexBaseDTO();
                    BeanUtils.copyProperties(m,a);
                    pList.add(a);
               });
               dto.setAnnexList(pList);
          }
          return dto;
     }

     @Override
     public List<PraxisProjectTypeDTO> getPraxisProjectTypeList() {
          PraxisProjectTypeExportReqVO reqVO=new PraxisProjectTypeExportReqVO ();
          reqVO.setStatus(1);
          List<PraxisProjectTypeDO> list = praxisProjectTypeService.getPraxisProjectTypeList(reqVO);
          List<PraxisProjectTypeDTO> rList=new ArrayList<>();
          if(CollectionUtils.isNotEmpty(list)){
               list.stream().forEach(m->{
                    PraxisProjectTypeDTO projectTypeDTO=new PraxisProjectTypeDTO();
                    BeanUtils.copyProperties(m,projectTypeDTO);
                    rList.add(projectTypeDTO);
               });
          }
          return rList;
     }

     @Override
     public PraxisProjectRespDTO getPraxisProjectById(Long id) {
          PraxisProjectDO praxisProject = praxisProjectService.getPraxisProject(id);
          PraxisProjectRespDTO respDTO=new PraxisProjectRespDTO();
          if(null!=praxisProject){
               BeanUtils.copyProperties(praxisProject,respDTO);
          }
          Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
          if(null!=loginUserId){
               praxisProjectService.updateViewNum(id,loginUserId);
          }
          return respDTO;
     }



     @Override
     public PraxisProjectRespDTO getPraxisProjectInfoById(Long id,Integer scheduleType) {
          PraxisProjectRespVO r = praxisProjectService.getPraxisProjectInfoById(id,scheduleType);
          PraxisProjectRespDTO d=new PraxisProjectRespDTO();
          if(null!=r){
               BeanUtils.copyProperties(r,d);
          }
          return d;
     }

     @Override
     public PageResult<PraxisProjectRespDTO> getPraxisProjectPage(PraxisProjectPageReqDTO pageReqDTO) {
          PraxisProjectPageReqVO pageReqVO=new PraxisProjectPageReqVO();
          BeanUtils.copyProperties(pageReqDTO,pageReqVO);
          pageReqVO.setQueryType(3);
          pageReqVO.setStatus(1);
          pageReqVO.setExamineStatus("1");
          pageReqVO.setShowFlag(1);
          PageResult<PraxisProjectRespVO> page = praxisProjectService.getPraxisProjectPage(pageReqVO);
          List<PraxisProjectRespVO> list = page.getList();
          PageResult<PraxisProjectRespDTO> pageResult=new PageResult<>();
          if(CollectionUtils.isNotEmpty(list)){
               pageResult.setTotal(page.getTotal());
               List<PraxisProjectRespDTO> rList=new ArrayList<>();
               list.stream().forEach(m->{
                    PraxisProjectRespDTO r=new PraxisProjectRespDTO();
                    BeanUtils.copyProperties(m,r);
                    rList.add(r);
               });
               pageResult.setList(rList);
          }
          return pageResult;
     }

     @Override
     public List<PraxisAnnexBaseDTO> getPraxisAnnexList(Long relationId, Integer relationType) {
          List<PraxisAnnexVO> pList = praxisProjectService.getPraxisAnnexListByProjectId(relationId, relationType);
          List<PraxisAnnexBaseDTO> aList=new ArrayList<>();
          if(CollectionUtils.isNotEmpty(pList)){
               pList.stream().forEach(m->{
                    PraxisAnnexBaseDTO b=new PraxisAnnexBaseDTO();
                    BeanUtils.copyProperties(m,b);
                    aList.add(b);
               });
          }
          return aList;
     }

     @Override
     public List<PraxisProjectScheduleDTO> getPraxisProjectScheduleList(Long projectId) {
          List<PraxisProjectScheduleVO> pList = praxisProjectService.getScheduleListByProjectId(projectId);
          List<PraxisProjectScheduleDTO> aList=new ArrayList<>();
          if(CollectionUtils.isNotEmpty(pList)){
               pList.stream().forEach(m->{
                    PraxisProjectScheduleDTO b=new PraxisProjectScheduleDTO();
                    BeanUtils.copyProperties(m,b);
                    aList.add(b);
               });
          }
          return aList;
     }
}
